package com.mashibing.juc.c_006;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @ClassName T02_CyclicBarrier
 * @Description CyclicBarrier，满20个线程就发车，100个线程就是5个启发，如果是99个？最后一组会因为少一个线程进入阻塞
 * @Author xiongwei.wu
 * @Date 2021/7/12 14:55
 **/
public class T02_CyclicBarrier {
    public static void main(String[] args) {
//        CyclicBarrier barrier = new CyclicBarrier(20);

        CyclicBarrier barrier = new CyclicBarrier(20, () -> {
            System.out.println("满人，发车");
        });

//        CyclicBarrier barrier = new CyclicBarrier(20, new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("满人，发车");
//            }
//        });

        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                try {
                    barrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();

        }
    }
}
